///////////////////////////////////////////////////////////////
// File: "CEquipmentState.cpp"
// Author: Ryan Burkevich (RB)
// Purpose: To contain all equipment select functionality.
///////////////////////////////////////////////////////////////

#include "CEquipmentState.h"
#include "CProfileManager.h"
#include "CBitmapFontEngine.h"
#include "CWiimote.h"
#include "CGame.h"
#include "CLevelSelectState.h"
#include "../Wrappers/CSGD_DirectInput.h"
#include "../Wrappers/CSGD_TextureManager.h"
#include "CSoundManager.h"
#include "CTransitionScreen.h"

CEquipmentState::CEquipmentState()
{
	m_nLRChoice = 0;
	m_nUDChoice = 0;
	m_nFontID = -1;
	for(int i = 0; i < 7; i++)
		m_vPowerUpIDs.push_back(-1);
}

CEquipmentState::~CEquipmentState()
{

}

void CEquipmentState::Enter()
{
	m_nFontID = CBitmapFontEngine::GetInstance()->LoadFont("BS_TechDemoTitle.xml");
	m_cScreen.LoadScreen("Screens/BS_LevelSelectScreen.xml");
	m_vPowerUpIDs[BALLOON] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Tiles/BS_PowerUp_Balloon.bmp");
	m_vPowerUpIDs[BRICK] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Tiles/BS_PowerUp_Brick.bmp");
	m_vPowerUpIDs[GOO] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Tiles/BS_PowerUp_Goo.bmp");
	m_vPowerUpIDs[METAL_BOOTS] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Tiles/BS_PowerUp_MetalBoots.bmp");
	m_vPowerUpIDs[MIRROR] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Tiles/BS_PowerUp_Mirror.bmp");
	m_vPowerUpIDs[PARACHUTE] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Tiles/BS_PowerUp_Parachute.bmp");
	m_vPowerUpIDs[SPRING] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Tiles/BS_PowerUp_Spring.bmp");
	
	m_nSelectedPwrUp = CProfileManager::GetInstance()->GetCurrentProfile()->GetCurrentPwrUp();
	m_bItsEquipTime = false;
}
		
void CEquipmentState::Exit()
{
	for(int i = 0; i < 7; i++)
		CSGD_TextureManager::GetInstance()->ReleaseTexture(m_vPowerUpIDs[i]);
	m_bItsEquipTime = false;
}
	
bool CEquipmentState::Input(float fElapsedTime)
{
	CWiimote* pWii = CWiimote::GetInstance();
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	CProfileManager* pPM = CProfileManager::GetInstance();
	
	if (pWii->IsButtonJustPressed(WII_HOME) || pDI->KeyPressed(DIK_ESCAPE))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
		CGame::GetInstance()->ChangeState(CLevelSelectState::GetInstance());
	}

	if(pWii->IsButtonJustPressed(WII_LEFT) || pDI->KeyPressed(DIK_LEFT))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
		m_nLRChoice--;
	}

	else if(pWii->IsButtonJustPressed(WII_RIGHT) || pDI->KeyPressed(DIK_RIGHT))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
		m_nLRChoice++;
	}

	if(m_bItsEquipTime)
	{
		if (m_nLRChoice > 1)
			m_nLRChoice = 0;
		else if (m_nLRChoice < 0)
			m_nLRChoice = 1;
	}
	else
	{
		if(m_nLRChoice > 8)
			m_nLRChoice = 0;
		else if(m_nLRChoice < 0)
			m_nLRChoice = 8;
	}

	if (pWii->IsButtonJustPressed(WII_A) || pDI->KeyPressed(DIK_RETURN))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
		if(!m_bItsEquipTime)
		{
			//Equip Balloon
			if(m_nLRChoice == BALLOON)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(BALLOON))
				{
					m_bItsEquipTime = true;
					m_nSelectedPwrUp = BALLOON;
					ResetChoices();
				}
			}//Equip Brick
			else if(m_nLRChoice == BRICK)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(BRICK))
				{
					m_bItsEquipTime = true;
					m_nSelectedPwrUp = BRICK;
					ResetChoices();
				}
			}//Equip Goo
			else if(m_nLRChoice == GOO)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(GOO))
				{
					m_bItsEquipTime = true;
					m_nSelectedPwrUp = GOO;
					ResetChoices();
				}
			}//Equip Metal Boots
			else if(m_nLRChoice == METAL_BOOTS)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(METAL_BOOTS))
				{
					m_bItsEquipTime = true;
					m_nSelectedPwrUp = METAL_BOOTS;
					ResetChoices();
				}
			}//Equip Mirror
			else if(m_nLRChoice == MIRROR)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(MIRROR))
				{
					m_bItsEquipTime = true;
					m_nSelectedPwrUp = MIRROR;
					ResetChoices();
				}
			}//Equip Parachute
			else if(m_nLRChoice == PARACHUTE)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(PARACHUTE))
				{
					m_bItsEquipTime = true;
					m_nSelectedPwrUp = PARACHUTE;
					ResetChoices();
				}
			}//Equip Spring
			else if(m_nLRChoice == SPRING)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(SPRING))
				{
					m_bItsEquipTime = true;
					m_nSelectedPwrUp = SPRING;
					ResetChoices();
				}
			}//UnEquip PwrUp
			else if(m_nLRChoice == NONE)
			{
				m_nSelectedPwrUp = NONE;
				pPM->GetCurrentProfile()->SetCurrentPwrUp(NONE);
				ResetChoices();
			}//Go Back to level select
			else if(m_nLRChoice == DONE)
			{
				CGame::GetInstance()->ChangeState(CLevelSelectState::GetInstance());
			}
		}//Equip PowerUp? Yes
		else if(m_bItsEquipTime && m_nLRChoice == 0)
		{
			pPM->GetCurrentProfile()->SetCurrentPwrUp(m_nSelectedPwrUp);
			m_bItsEquipTime = false;
			ResetChoices();
		}//Equip PowerUp? No
		else if(m_bItsEquipTime && m_nLRChoice == 1)
		{
			m_bItsEquipTime = false;
			m_nSelectedPwrUp = pPM->GetCurrentProfile()->GetCurrentPwrUp();
			ResetChoices();
		}
	}
	return true;	
}

void CEquipmentState::Update(float fElapsedTime)
{

}

void CEquipmentState::Render()
{
	m_cScreen.RenderScreen();

	CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();
	CProfileManager* pPM = CProfileManager::GetInstance();

	int TopX= m_cScreen.GetPosObjMembers("Top",XPOS);
	int TopY= m_cScreen.GetPosObjMembers("Top",YPOS);

	int LeftX = m_cScreen.GetPosObjMembers("Left",XPOS);
	int LeftY = m_cScreen.GetPosObjMembers("Left",YPOS);

	int RightX = m_cScreen.GetPosObjMembers("Right",XPOS);
	int RightY = m_cScreen.GetPosObjMembers("Right",YPOS);

	pBFE->RenderString("Equipment", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), TopX+40, TopY+25, 0);

	if(!m_bItsEquipTime)
	{
		if(m_nSelectedPwrUp != NONE)
		{
			pBFE->RenderString("Current", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX, RightY, 0);
			CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[m_nSelectedPwrUp], RightX+60, RightY+60, 3, 3, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Power Up", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX, RightY+200, 0);
		}

		int PowerUpX = LeftX+15;
		int PowerUpY = LeftY+15;

		pBFE->RenderString("None", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), PowerUpX+70, PowerUpY+155, 0);
		pBFE->RenderString("Done", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), PowerUpX+70, PowerUpY+190, 0);

		switch(m_nLRChoice)
		{
		case BALLOON:
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(BALLOON))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[BALLOON], PowerUpX, PowerUpY, 2, 2, 0, 0, 0, 0, 0xffffffAA);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[BALLOON], PowerUpX, PowerUpY, 2, 2, 0, 0, 0, 0, 0xff646432);				
				break;
			}
		case BRICK:
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(BRICK))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[BRICK], PowerUpX+70, PowerUpY, 2, 2, 0, 0, 0, 0, 0xffffffAA);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[BRICK], PowerUpX+70, PowerUpY, 2, 2, 0, 0, 0, 0, 0xff646432);
				break;
			}		
		case GOO:
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(GOO))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[GOO], PowerUpX+140, PowerUpY, 2, 2, 0, 0, 0, 0, 0xffffffAA);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[GOO], PowerUpX+140, PowerUpY, 2, 2, 0, 0, 0, 0, 0xff646432);
				break;
			}		
		case METAL_BOOTS:
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(METAL_BOOTS))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[METAL_BOOTS], PowerUpX, PowerUpY+70, 2, 2, 0, 0, 0, 0, 0xffffffAA);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[METAL_BOOTS], PowerUpX, PowerUpY+70, 2, 2, 0, 0, 0, 0, 0xff646432);
				break;
			}		
		case MIRROR:
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(MIRROR))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[MIRROR], PowerUpX+70, PowerUpY+70, 2, 2, 0, 0, 0, 0, 0xffffffAA);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[MIRROR], PowerUpX+70, PowerUpY+70, 2, 2, 0, 0, 0, 0, 0xff646432);
				break;
			}		
		case PARACHUTE:
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(PARACHUTE))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[PARACHUTE], PowerUpX+140, PowerUpY+70, 2, 2, 0, 0, 0, 0, 0xffffffAA);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[PARACHUTE], PowerUpX+140, PowerUpY+70, 2, 2, 0, 0, 0, 0, 0xff646432);
				break;
			}		
		case SPRING:
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(SPRING))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[SPRING], PowerUpX, PowerUpY+140, 2, 2, 0, 0, 0, 0, 0xffffffAA);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[SPRING], PowerUpX, PowerUpY+140, 2, 2, 0, 0, 0, 0, 0xff646432);
				break;
			}
		case NONE:
			{
				pBFE->RenderString("None", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 0), PowerUpX+70, PowerUpY+155, 0);
				break;
			}
		case DONE:
			{
				pBFE->RenderString("Done", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 0), PowerUpX+70, PowerUpY+190, 0);
				break;
			}
		}

		int nXPos = 0, nYPos = 0;
		for(int i = 0; i < 7; i++)
		{
			if (i == 6)
			{
				nXPos = 0;
				nYPos = 140;
			}
			if(i != m_nLRChoice)
			{
				if(pPM->GetCurrentProfile()->CheckForPwrUp(i))
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[i], PowerUpX + nXPos, PowerUpY + nYPos, 2, 2, 0, 0, 0, 0, 0xffffffff);
				else
					CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[i], PowerUpX + nXPos, PowerUpY + nYPos, 2, 2, 0, 0, 0, 0, 0xff323232);
			}
			nXPos += 70;
			if(i == 2)
			{
				nXPos = 0;
				nYPos = 70;
			}
		}
	}
	else if(m_bItsEquipTime)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_vPowerUpIDs[m_nSelectedPwrUp], LeftX+60, LeftY+60, 3, 3, 0, 0, 0, 0, 0xffffffff);

		pBFE->RenderString("Equip:",m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), LeftX+15, LeftY, 0);
		switch(m_nSelectedPwrUp)
		{
		case BALLOON:
			{
				pBFE->RenderString("Balloon", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX, RightY+64, 0);
				pBFE->RenderString(" -0:45", m_nFontID, 0, D3DCOLOR_XRGB(255, 0, 0), RightX, RightY+96, 0);
				break;
			}
		case BRICK:
			{
				pBFE->RenderString("Brick", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 255), RightX,  RightY+64, 0);
				pBFE->RenderString(" -0:15", m_nFontID, 0, D3DCOLOR_XRGB(255, 0, 0), RightX, RightY+96, 0);

				break;
			}		
		case GOO:
			{
				pBFE->RenderString("Goo", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 255), RightX,  RightY+64, 0);
				pBFE->RenderString(" -1:00", m_nFontID, 0, D3DCOLOR_XRGB(255, 0, 0), RightX, RightY+96, 0);
				break;
			}		
		case METAL_BOOTS:
			{
				pBFE->RenderString("Metal", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 255), RightX,  RightY+64, 0);
				pBFE->RenderString(" -0:45", m_nFontID, 0, D3DCOLOR_XRGB(255, 0, 0), RightX, RightY+96, 0);
				break;
			}		
		case MIRROR:
			{
				pBFE->RenderString("Mirror", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 255), RightX,  RightY+64, 0);
				pBFE->RenderString(" -0:25", m_nFontID, 0, D3DCOLOR_XRGB(255, 0, 0), RightX, RightY+96, 0);
				break;
			}		
		case PARACHUTE:
			{
				pBFE->RenderString("Parachute", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 255), RightX,  RightY+64, 0);
				pBFE->RenderString(" -0:25", m_nFontID, 0, D3DCOLOR_XRGB(255, 0, 0), RightX, RightY+96, 0);
				break;
			}		
		case SPRING:
			{
				pBFE->RenderString("Spring", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 255), RightX,  RightY+64, 0);
				pBFE->RenderString(" -0:30", m_nFontID, 0, D3DCOLOR_XRGB(255, 0, 0), RightX, RightY+96, 0);
				break;
			}
		}
		switch(m_nLRChoice)
		{
		case 0:
			{
				pBFE->RenderString("Yes", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 0), LeftX, LeftY+188, 0);
				pBFE->RenderString("No", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), LeftX+128, LeftY+188, 0);
				break;
			}
		case 1:
			{
				pBFE->RenderString("Yes", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), LeftX, LeftY+188, 0);
				pBFE->RenderString("No", m_nFontID, 0, D3DCOLOR_XRGB(255, 255, 0), LeftX+128, LeftY+188, 0);
				break;
			}
		}	
	}
}